<?php
/**
 * @file $HeadURL: keyword.php $
 * @author $Author: Jim.chou (zhouzhe@haibao.com) $
 * @date $Date: 2015-3-28 15:19:18 $
 * @brief 
 */
namespace haibao\cms\data;

use \haibao\cms\model\filter\Keyword as KeywordFilter;
use haibao\frame\data\query\Condition;
use haibao\cms\web\view\app\article\Preview;

class Keyword extends \haibao\cms\data\BaseMysql{
	
	public function __construct(){
		parent::__construct('\haibao\cms\model\data\Keyword');
	}
	
	public function addKeyword($models){
		$this->addAll($models);
	}
	public function addKeywordOne($model){
		$this->add($model);
	}
	public function getModelById($KeywordId){
		return $this->getOneById($KeywordId);
	}
	
	public function getKeywordList($KeywordFilter){
		return parent::getAll($KeywordFilter);
	}
	
	public function delKeyword($id){
		$this->updateById($id, array("Status"=>0));
	}
	public function activateKeyword($id){
		$this->updateById($id, array("Status"=>1));
	}
	public function getKeywordsByIds($ids){
	    $keywordsArr = array();
	    if(is_array($ids) && !empty($ids)){
	        $str = preg_replace('/(\d)+/i', '%s', implode(',', $ids));
	        $sql = "select Id,Name from KeyWord where Id IN (".$str.") and Status = 1";
	        $result = $this->query($sql,$ids);
	        while($result->fetch_assoc()){
	            $keywordsArr[$result->getData('Id')] = $result->getData('Name');
	        }
	    }
		return $keywordsArr;
	}
	public function checkNameUnique($keywordModelArr){
		$keywordFilter = new KeywordFilter();
		$keywordFilter->where($keywordFilter::CONDITION_FIELD_NAME_NAME,Condition::CONDITION_IN,array_map('strtolower', $keywordModelArr));
		$keywordFilter->where($keywordFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_EQUAL,\haibao\cms\model\data\Keyword::DEFAULT_STATUS_OK);
		$keywordModel = $this->getAll($keywordFilter);
		$resModelArr = array();
		foreach($keywordModelArr as $key=>$value){
			foreach ($keywordModel as $key=>$model){
				if(strtolower($value) == strtolower($model->Name)){
					$resModelArr[$model->Id] = $value;
				}
			}
		}
		
		return $resModelArr;
	}
	
}